package com.web;

import com.MessageService;
import com.domain.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.naming.NamingException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/displayMessages")
public class DisplayMessagesController {
    @Autowired
    private MessageService messageService;

    @RequestMapping("/displayMessages")
    public ModelAndView displayUsers(HttpServletRequest request, HttpServletResponse response) {
        List<Message> messages = new ArrayList<Message>();
        try {
            messages = messageService.getMessages();
        } catch (ClassNotFoundException | SQLException | NamingException | IOException e) {
            e.printStackTrace();
        }
        return new ModelAndView("displayMessages", "messages", messages);
    }
}
